package com.zeroext.show.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import com.zeroext.action.common.BaseAction;
import com.zeroext.common.util.JsonUtil;
import com.zeroext.common.util.JsonWapper;
import com.zeroext.show.entity.EmailMsgWapper;
import com.zeroext.sys.util.DateUtil;
import com.zeroext.sys.util.EmailUtil;
/**
 * Email Action
 * @author ouch
 *
 */
@SuppressWarnings("serial")
public class EmailAction extends BaseAction {
    /**
     * send eamil
     * @return
     * @throws Exception
     */
    public String sendEmail()throws Exception{
        HttpServletRequest request = this.getRequest();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String url = request.getParameter("url");
        String from = request.getParameter("from");
        String to = request.getParameter("to");
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        
        boolean isSuccess = EmailUtil.sendEmail(username, password, url, from, to, title, content);
        JSONObject jo = new JSONObject();
        jo.put("success", isSuccess);
        JsonUtil.writeJson(this.getResponse(), jo.toString());
        return null;
    }
    /**
     * recevice email
     * @return
     * @throws Exception
     */
    public String receiveEamil()throws Exception{
        HttpServletRequest request = this.getRequest();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String url = request.getParameter("url");
        String lastCountStr = request.getParameter("lastCount");
        int lastCount = 10;
        try{
            lastCount = Integer.parseInt(lastCountStr);
        }catch(Exception e){}
        if(lastCount>30){
            lastCount = 30;
        }
        List<EmailMsgWapper> result = EmailUtil.listEmail(url, username, password, lastCount);
        this.getSession().setAttribute("myEmails", result);
        JsonWapper jw = new JsonWapper();
        for(EmailMsgWapper w : result){
            JSONObject jo = jw.nextItem();
            jo.put("id", w.getId());
            jo.put("title", w.getTitle());
            jo.put("from", w.getFrom());
            jo.put("receiveDate", DateUtil.getYYMMDDHHMMSS(w.getSendDate()));
            jw.endItem(jo);
        }
        jw.writeStore(this.getResponse(), result.size());
        return null;
    }
    
    /**
     * get email detail
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public String getEmailDetail()throws Exception{
        String id = this.getRequest().getParameter("id");
        List<EmailMsgWapper> result = (List<EmailMsgWapper>)this.getSession().getAttribute("myEmails");
        if(result == null){
            JsonUtil.writeFailedJson(this.getResponse());
            return null;
        }
        EmailMsgWapper w = null;
        for(EmailMsgWapper wIt : result){
            if(wIt.getId().equals(id)){
                w = wIt;
                break;
            }
        }
        if(w == null){
            JsonUtil.writeFailedJson(this.getResponse());
            return null;
        }
        JSONObject jo = JSONObject.fromObject(w);
        JsonUtil.writeJson(this.getResponse(), jo.toString());
        return null;
    }
}
